import 'dart:math';

import 'package:flutter/material.dart';
import 'package:ls_loading/Type.dart';

class LoadingPainter extends CustomPainter {
  int index;
  List<int> pointsArc;
  final LoadingStyle loadingStyle;
  late Paint _paint;
  LoadingPainter({
    required this.index,
    required this.pointsArc,
    required this.loadingStyle,
  }) {
    _paint = Paint()
      ..color = loadingStyle.color
      ..style = PaintingStyle.fill
      ..strokeJoin = StrokeJoin.round;
  }

  @override
  void paint(Canvas canvas, Size size) {
    const opacity = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8];
    // 画圆
    var path = Path();
    for (var i = 0; i < opacity.length; i++) {
      var element = pointsArc[i];
      var x = size.width / 2 + loadingStyle.radius * cos(element * pi / 180);
      var y = size.height / 2 + loadingStyle.radius * sin(element * pi / 180);
      // 修改每个点不透明度
      _paint.color = Color.fromRGBO(
          _paint.color.red, _paint.color.green, _paint.color.blue, opacity[i]);
      path.addOval(Rect.fromCircle(
          center: Offset(x, y), radius: loadingStyle.strokeWidth));
      canvas.drawPath(path, _paint);
      //清空path
      path = Path();
    }
  }

  @override
  bool shouldRepaint(LoadingPainter oldDelegate) {
    return oldDelegate.index != index;
  }
}
